var models = require('../db')
var express = require('express')
var router = express.Router()
var mysql = require('mysql')
var $sql = require('../sqlMap')

var conn = mysql.createConnection(models.mysql)

conn.connect()
var jsonWrite = function (res, ret) {
    if (typeof ret === 'undefined') {
        res.json({
        code: '1',
        msg: '操作失败'
        })
    } else {
        res.json(ret)
    }
}

router.get('/getFlightList',(req,res) =>{
    var params = req.query || req.params
    let start = (params.page - 1) * 10;
    var sql = $sql.flight.count;
    let allCount = 0;
    conn.query(sql,function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.flight.get;
            conn.query(sql,[start],function(err,result){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var list = result;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:list
                    }))
                }
            })
        }
    })
})

router.get('/searchFlightList',(req,res) =>{
    var params = req.query || req.params
    let plane_id = parseInt(params.plane_id);
    let start = (params.page - 1) * 10;
    var sql = $sql.flight.searchCount;
    let allCount = 0;
    conn.query(sql,[plane_id],function(err,result){
        if(err){
            console.log(err);
        }else{
            console.log(result)
            allCount = result[0]['count(*)'];
            sql = $sql.flight.searchList;
            conn.query(sql,[plane_id,start],function(err,result){
                if(err){
                    console.log(err);
                    throw err;
                }else{
                    var list = result;
                    res.send(JSON.stringify({
                        msg:"操作成功",
                        status:200,
                        count:allCount,
                        list:list
                    }))
                }
            })
        }
    })
})

router.get('/getFlight',(req,res) =>{
    var params = req.query || req.params
    var sql = $sql.flight.search;
    params.flight_id = parseInt(params.flight_id);
    conn.query(sql,[params.flight_id],function(err,result){
        if(err){
            console.log(err);
        }
        if(result){
            console.log(result)
            res.send(JSON.stringify({
                data:result[0]
            }))
        }
    })
})

router.post('/addFlight',(req,res)=>{
    var sql = $sql.plane.search
    var params = req.body
    params.startTime = new Date(params.startTime);
    params.arriveTime = new Date(params.arriveTime);
    params.create_time = new Date();
    params.modify_time = new Date();
    params.admin_id = parseInt(params.admin_id);
    params.plane_id = parseInt(params.plane_id);
    params.touristClassPrice = parseInt(params.touristClassPrice);
    params.businessClassPrice = parseInt(params.businessClassPrice);
    params.firstClassPrice = parseInt(params.firstClassPrice);
    console.log(params);
    conn.query(sql,[params.plane_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            console.log(result);
            if(result[0]){
                params.touristClassRemain = result[0].touristClassNum;
                params.businessClassRemain = result[0].businessClassNum;
                params.firstClassRemain = result[0].firstClassNum;

                sql = $sql.flight.add
                console.log(params)
                conn.query(sql,[params], function (err, addResult) {
                    if (err) {
                        console.log(err);
                        throw(err);
                    }
                    if (addResult) {
                        res.send(JSON.stringify({
                            msg:"操作成功",
                            status:200
                        }))
                    }
                })
            }else{
                res.send(JSON.stringify({
                    msg:"该飞机不存在",
                    status:402,
                }))
            }
        }
    })
})

router.post('/editFlight',(req,res)=>{
    var sql = $sql.flight.update
    var params = req.body

    params.admin_id = parseInt(params.admin_id);
    params.flight_id = parseInt(params.flight_id);
    params.plane_id = parseInt(params.plane_id);
    params.startTime = new Date(params.startTime);
    params.arriveTime = new Date(params.arriveTime);
    params.create_time = new Date(params.create_time);
    params.modify_time = new Date();

    params.touristClassPrice = parseInt(params.touristClassPrice);
    params.businessClassPrice = parseInt(params.businessClassPrice);
    params.firstClassPrice = parseInt(params.firstClassPrice);
    params.touristClassRemain = parseInt(params.touristClassRemain);
    params.businessClassRemain = parseInt(params.businessClassRemain);
    params.firstClassRemain = parseInt(params.firstClassRemain);
    params.state = parseInt(params.state);
    params.is_del = 0;
    conn.query(sql,[params,params.flight_id], function (err, result) {
        if (err) {
            console.log(err);
            throw err;
        }
        if (result) {
            console.log(result)
            res.send(JSON.stringify({
                msg:"操作成功",
                status:200
            }))
        }
    })
})

router.post('/deleteFlight',(req,res)=>{
    var sql = $sql.flight.update
    var params = req.body
    params.modify_time = new Date();
    params.admin_id = parseInt(params.admin_id);
    params.is_del = 1;
    console.log(params)
    conn.query(sql, [params,params.flight_id], function (err, result) {
        if (err) {
            console.log(err)
        }
        if (result) {
            res.send(JSON.stringify({
                msg:"操作成功",
                status:200
            }))
        }
    })
})

module.exports = router;